
namespace Pixels.ExtentedListItems.DataLayer
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    using Pixels.DataLayer;
    using Pixels.Exceptions;

    public class DataAccessLayer : IDataAccessLayer
    {
        private DbHelper helper;

        #region Constructors
        public DataAccessLayer()
        {
            helper = new CommonDbHelper();
        }
        public DataAccessLayer(string ConnectionStringName)
        {
            helper = new CommonDbHelper(ConnectionStringName);
        }
        #endregion

        #region Methods
        public IExtentedListItem ExtentedListItemAddEdit(IExtentedListItem objExtentedListItem)
        {
            try
            {
                IExtentedListItem _extentedListItem = ExtentedListItemFetch(objExtentedListItem.ItemName, objExtentedListItem.ListItemID.Value, objExtentedListItem.ContentTypeItemID.Value);

                if (_extentedListItem == null)
                {
                    helper.ExecuteNonQuery(SqlQueryBuilder.GetInsertQuery(objExtentedListItem), CommandType.Text);
                }
                else
                {
                    objExtentedListItem.ExtentedListItemID = _extentedListItem.ExtentedListItemID;
                    helper.ExecuteNonQuery(SqlQueryBuilder.GetUpdateQuery(objExtentedListItem, "ExtentedListItemID"), CommandType.Text);
                }

                return objExtentedListItem;
            }
            catch (DbException DbEx)
            {
                bool rethrow = ExceptionPolicy.HandleException(DbEx, "Data Layer Policy");
                throw new DLWrappedException("ExtentedListItem could not be added/modified. Please Contact System Administrator");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        public void ExtentedListItemDelete(Guid ExtentedListItemID)
        {
            try
            {
                helper.ExecuteNonQuery(DBConstants.ExtentedListItemDelete, ExtentedListItemID);
            }
            catch (DbException DbEx)
            {
                bool rethrow = ExceptionPolicy.HandleException(DbEx, "Data Layer Policy");
                throw new DLWrappedException("ExtentedListItem not be deleted");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        public IExtentedListItem ExtentedListItemFetch(Guid ExtentedListItemID)
        {
            ExtentedListItem objExtentedListItem = null;
            try
            {
                using (IDataReader reader = helper.ExecuteReader(DBConstants.ExtentedListItemFetchByID, ExtentedListItemID))
                {
                    if ((reader != null) && (reader.Read()))
                    {
                        objExtentedListItem = new ExtentedListItem();

                        LoadToExtentedListItem(objExtentedListItem, reader);
                    }
                    reader.Close();
                    return objExtentedListItem;
                }
            }
            catch (DbException DbEx)
            {
                bool rethrow = ExceptionPolicy.HandleException(DbEx, "Data Layer Policy");
                throw new DLWrappedException("ExtentedListItem Detail could not be found");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }       

        public IExtentedListItem ExtentedListItemFetch(string ItemName, Guid ListItemID, Guid ContentTypeItemID)
        {
            ExtentedListItem objExtentedListItem = null;
            try
            {
                using (IDataReader reader = helper.ExecuteReader(DBConstants.ExtentedListItemFetchByItemName_ListItemID_ContentTypeItemID, ItemName, ListItemID, ContentTypeItemID))
                {
                    if ((reader != null) && (reader.Read()))
                    {
                        objExtentedListItem = new ExtentedListItem();

                        LoadToExtentedListItem(objExtentedListItem, reader);
                    }
                    reader.Close();
                    return objExtentedListItem;
                }
            }
            catch (DbException DbEx)
            {
                bool rethrow = ExceptionPolicy.HandleException(DbEx, "Data Layer Policy");
                throw new DLWrappedException("ExtentedListItem Detail could not be found");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }   

        public IList<IExtentedListItem> ExtentedListItemsFetchByListItemID(Guid ListItemID)
        {
            List<IExtentedListItem> objExtentedListItems = null;
            try
            {
                using (IDataReader reader = helper.ExecuteReader(DBConstants.ExtentedListItemsFetchByListItemID, ListItemID))
                {
                    if ((reader != null))
                    {
                        objExtentedListItems = new List<IExtentedListItem>();
                        while ((reader.Read()))
                        {
                            ExtentedListItem objExtentedListItem = new ExtentedListItem();

                            LoadToExtentedListItem(objExtentedListItem, reader);

                            objExtentedListItems.Add(objExtentedListItem);
                        }
                    }
                    reader.Close();
                    return objExtentedListItems;
                }
            }
            catch (DbException DbEx)
            {
                bool rethrow = ExceptionPolicy.HandleException(DbEx, "Data Layer Policy");
                throw new DLWrappedException("ExtentedListItem Detail could not be found");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        public IList<IExtentedListItem> ExtentedListItemsFetchByContentItemID(Guid ContentItemID)
        {
            ExtentedListItem objExtentedListItem = null;
            try
            {
                using (IDataReader reader = helper.ExecuteReader(DBConstants.ExtentedListItemFetchByID, ContentItemID))
                {
                    if ((reader != null) && (reader.Read()))
                    {
                        objExtentedListItem = new ExtentedListItem();

                        LoadToExtentedListItem(objExtentedListItem, reader);
                    }
                    reader.Close();
                    return null;
                }
            }
            catch (DbException DbEx)
            {
                bool rethrow = ExceptionPolicy.HandleException(DbEx, "Data Layer Policy");
                throw new DLWrappedException("ExtentedListItem Detail could not be found");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        public IList<IExtentedListItem> ExtentedListItemsFetch()
        {
            IList<IExtentedListItem> ExtentedListItems = null;
            try
            {
                using (IDataReader reader = helper.ExecuteReader(DBConstants.ExtentedListItemsFetch, CommandType.Text))
                {
                    if ((reader != null))
                    {
                        ExtentedListItems = new List<IExtentedListItem>();
                        while (reader.Read())
                        {
                            ExtentedListItem objExtentedListItem = new ExtentedListItem();

                            LoadToExtentedListItem(objExtentedListItem, reader);

                            ExtentedListItems.Add(objExtentedListItem);
                        }
                    }
                    reader.Close();
                    return ExtentedListItems;
                }
            }
            catch (DbException DbEx)
            {
                bool rethrow = ExceptionPolicy.HandleException(DbEx, "Data Layer Policy");
                throw new DLWrappedException("Error in Fetching ExtentedListItem Details.");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        private void LoadToExtentedListItem(ExtentedListItem objExtentedListItem, IDataReader reader)
        {
            objExtentedListItem.ExtentedListItemID = Guid.Parse(reader["ExtentedListItemID"].ToString());
            objExtentedListItem.ItemName = reader["ItemName"].ToString();
            objExtentedListItem.Value = reader["Value"].ToString();
            Guid _contenttypeitemid;
            Guid.TryParse(reader["ContentTypeItemID"] == null ? "" : reader["ContentTypeItemID"].ToString(), out _contenttypeitemid);
            objExtentedListItem.ContentTypeItemID = _contenttypeitemid;            
            objExtentedListItem.ListItemID = Guid.Parse(reader["ListItemID"].ToString());

        }
        #endregion
    }
}
